package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.DmcGatewayDataMapper;
import com.csun.cmny.provider.device.csun.gateway.entity.CsunGatewayData;
import com.csun.cmny.provider.exceptions.DmcBizException;
import com.csun.cmny.provider.model.domain.DmcDevice;
import com.csun.cmny.provider.model.domain.DmcGatewayData;
import com.csun.cmny.provider.service.DmcDeviceService;
import com.csun.cmny.provider.service.DmcGatewayDataService;
import com.csun.cmny.util.PublicUtil;
import com.google.common.base.Preconditions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-01-16 13:44
 * @Description: the class dmc gatewat data service impl
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DmcGatewayDataServiceImpl extends BaseService<DmcGatewayData> implements DmcGatewayDataService {

    @Resource
    private DmcGatewayDataMapper gatewayDataMapper;

    @Resource
    private DmcDeviceService dmcDeviceService;

    /**
     * query real gateway data by device id
     *
     * @param deviceId the device id
     *
     * @return the dmc gateway data
     */
    @Override
    public DmcGatewayData queryRealGatewayDataByDeviceId(Long deviceId) {

        Preconditions.checkArgument(deviceId != null, "设备Id不能为空");

        DmcDevice dmcDeviceQuery = new DmcDevice();
        dmcDeviceQuery = dmcDeviceService.selectByKey(deviceId);
        if (PublicUtil.isEmpty(dmcDeviceQuery)) {
            throw new DmcBizException(ErrorCodeEnum.DMC100610002, deviceId);
        }

        return gatewayDataMapper.selectDeviceDataByDeviceId(deviceId);
    }

    /**
     * update gatewaydata by device id
     *
     * @param deviceId the device id
     * @return the int
     */
    @Override
    public Integer updateGatewayDataByDeviceId(Long deviceId) {
        return null;
    }

    /**
     * delete gateway data by device id
     *
     * @param deviceId the device id
     * @return the int
     */
    @Override
    public Integer deleteGatewayDataByDeviceId(Long deviceId) {
        return null;
    }

    /**
     * 查询智能网关数据列表
     *
     * @return
     */
    @Override
    public List<CsunGatewayData> queryGateWayDataList() {

        return gatewayDataMapper.queryGateWayDataList();
    }

    /**
     * save or update dmc gateway data
     *
     * @param dmcGatewayData the dmc gateway data
     *
     * @return the int.
     */
    @Override
    public Integer saveOrUpdate(DmcGatewayData dmcGatewayData) {

        return gatewayDataMapper.saveOrUpdate(dmcGatewayData);
    }
}
